package com.hbsx.why.ui.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hbsx.why.R;
import com.hbsx.why.adapter.LGRecycleViewAdapter;
import com.hbsx.why.adapter.LGViewHolder;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.base.BaseApplication;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.SpKeyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by liub
 * time 2017/8/28
 */

public class SearchActivity extends BaseActivity {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.search_recycleView)
    RecyclerView searchRecycleView;
    private String search_hostory;
    private List<String> stringList;
    private MyAdapter myAdapter;
    private String searchText;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_search_activity;
    }

    @Override
    protected void initView() {
        stringList = new ArrayList<>();
        search_hostory = BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.SEARCH_HISTORY);
        LogUtils.logi(searchText + "----search_hostory===" + search_hostory);
        if (search_hostory != null && !search_hostory.isEmpty()) {
            String[] arr = search_hostory.split(",");
            for (int i = 0; i < (arr.length > 10 ? 10 : arr.length); i++) {
                stringList.add(arr[i]);
            }
        }
        stringList.add("清空记录");
        setGridLayout(searchRecycleView, this, 1);
        myAdapter = new MyAdapter(stringList);
        searchRecycleView.setAdapter(myAdapter);
    }


    @OnClick({R.id.search_btn_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_btn_ll:
                searchText = etSearch.getText() + "";
                if (!stringList.contains(searchText)) {
                    if (!search_hostory.isEmpty()) {
                        search_hostory = "," + search_hostory;
                    }
                    search_hostory = searchText + search_hostory;
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.SEARCH_HISTORY, search_hostory);
                    stringList.add(0, searchText);
                    myAdapter.refreshDate(stringList);
                    myAdapter.notifyDataSetChanged();
                }

                LogUtils.logi(searchText + "----stringList===" + stringList.size());
                break;
        }
    }

    public class MyAdapter extends LGRecycleViewAdapter<String> {

        public MyAdapter(List<String> dataList) {
            super(dataList);
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_search_list;
        }

        @Override
        public void convert(LGViewHolder holder, final String s, int position) {
            TextView textView = (TextView) holder.getView(R.id.text_view);
            textView.setText(s);
            holder.getView(R.id.type_linear).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (s.equals("清空记录")) {
                        BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.SEARCH_HISTORY, "");
                        stringList.clear();
                        stringList.add("清空记录");
                        myAdapter.refreshDate(stringList);
                        myAdapter.notifyDataSetChanged();
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("searchtext", s);
                        //launch(SearchListActivity.class, intent);
                    }
                }
            });
        }
    }

    /**
     * GridLayoutManager
     */

    public void setGridLayout(RecyclerView pullRecycleView, Context mContext, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }
}
